import React, { Component,PropTypes} from 'react';
import {Link} from 'react-router';
import SplitLine from '../../Component/CommonComponent/SplitLine'
import {OrderDetail,ConfirmReceived} from '../../Action/auth';

export default class AgentOrderDetails extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            orderNo:'',
            orderFormdDetails:[],
            timer:'',
            address:'',
            mobile:'',
            name:'',
            detail:'',
            status:'',
            //confirmItem:[]
        };
    }

    static contextTypes = {
        router:PropTypes.object
    }

    componentWillMount(){
        let orderNo = this.props.location.query.orderId;
        this.setState({orderNo:orderNo})
        this.getOrderDetail(orderNo);
    }

    //订单详情
    async getOrderDetail(orderNo){
        await OrderDetail(orderNo)
            .then(res=>{
                this.setState({orderFormdDetails:res})
                this.setState({timer:res.plan_receive_time})
                this.setState({address:res.address})
                this.setState({detail:res.address_detail})
                this.setState({mobile:res.mobile})
                this.setState({name:res.name})

                if(res.status==6){
                    this.setState({status:'申请退款'})
                }else if(res.status==7){
                    this.setState({status:'退款成功'})
                }else if(res.status==8){
                    this.setState({status:'退款失败'})
                }else{
                    this.setState({status:''})
                }

                //地址从sessionStorage里面去拿
                if(this.props.location.query.isOrder){
                    this.setState({address:sessionStorage.getItem('getOrderAddress')?sessionStorage.getItem('getOrderAddress'):''})
                    this.setState({datail:sessionStorage.getItem('getOrderDetail')?sessionStorage.getItem('getOrderDetail'):''})
                    this.setState({name:sessionStorage.getItem('getOrderName')?sessionStorage.getItem('getOrderName'):''})
                    this.setState({mobile:sessionStorage.getItem('getOrderMobile')?sessionStorage.getItem('getOrderMobile'):''})
                    sessionStorage.removeItem("getOrderAddress")
                    sessionStorage.removeItem("getOrderDetail")
                    sessionStorage.removeItem("getOrderName")
                    sessionStorage.removeItem("getOrderMobile")
                }

                if(this.props.location.query.isOrderTime){
                    //时间从sessionStorage里面去拿
                    this.setState({timer:sessionStorage.getItem('orderTime')})
                    alert(this.state.timer)
                    //sessionStorage.removeItem("getOrderMobile")
                }

            })
            .catch(err=>{
                console.warn('err',err)
            })
    }

    check(orderNo){
        if(confirm("确认发货?")){
            const {orderFormdDetails} = this.state;
            console.log(orderFormdDetails)
            const list = orderFormdDetails.orderDetails;
            //for(var i=0;i<list.length;i++){
            //    if(list[i].WAREHOUSE_STOCK<list[i].NUM){
            //        alert('库存不足，请前往进货');
            //        this.context.router.push({pathname:'purchase'});
            //        return
            //    }
            //}
            this.context.router.push({pathname:"/goPicking",query:{orderNo:orderNo,type:1}})
        }else{
            return;
        }

    }

    render() {
        const {orderFormdDetails,timer,address,detail,mobile,name,orderNo} = this.state
        return (
            <div className="containerNav">
                <div className="flex flex-1 flex-pack-justify flex-align-center border_bottom" style={{padding:'5px 10px'}}>
                    <div className="flex flex-1 flex-align-center">
                        <span className="fl di" style={{lineHeight:0,marginRight:15,width:'0.6rem',height:'0.95rem'}}>
                            <img src={require('../../Images/location.png')} alt=""/>
                        </span>
                        {
                            address == null || address == ''?
                                <div style={{height:35,lineHeight:'35px'}}>
                                    <span className="color6 f14">完善收货地址</span>
                                </div>
                                :
                                <div className="flex-1 mtlr">
                                    <div className="flex flex-pack-justify f14 color6">
                                        <div><span>收货人:</span><span>{name}</span></div>
                                        <span className="di color6">{mobile}</span>
                                    </div>
                                    <p className="f12 color9" style={{margin:'5px 0 0'}}>
                                        收货地址:<span>{(address?address:'')+(detail?detail:'')}</span>
                                    </p>
                                </div>
                        }
                    </div>
                    <span className="di" style={{width:9,height:16,lineHeight:0,marginLeft:10}}>
                        <img src={require('../../Images/rightArrow.png')} alt=""/>
                    </span>
                </div>
                <div className="flex flex-pack-justify flex-align-center border_bottom" style={{height:50,padding:'5px 10px'}}>
                    <div className="flex flex-align-center">
                        <span className="di mr6 timeImg" style={{lineHeight:0,marginRight:15}}><img src={require('../../Images/time.png')} alt=""/></span>
                        {
                            timer ==''||timer == undefined ?
                                <span className="color6 f14">送货时间不限</span>
                                :
                                <span className="color6 f14">{this.state.timer}</span>
                        }
                    </div>
                   <span className="di" style={{width:9,height:16,lineHeight:0,marginLeft:10}}>
                        <img src={require('../../Images/rightArrow.png')} alt=""/>
                    </span>
                </div>
                <SplitLine />
                {
                    orderFormdDetails.orderDetails&&orderFormdDetails.orderDetails.map(el=>{
                        return(
                            <div className="posr plAll df border_bottom">
                                <div style={{width:'5rem',height:'5rem'}}>
                                    <img src={el.PRODUCT_IMAGE} alt=""/>
                                </div>
                                <div className="color6 flex-1 f14 order_margin">
                                    <p className="oh orderShow db">{el.PRODUCT_NAME}</p>
                                </div>
                                <div className="pa order_price tr">
                                    <p className="" style={{marginBottom:0}}><span className="f12">￥</span><span className="f15">
                                        {parseFloat(el.PRICE/100).toFixed(2)}</span></p>
                                    <p className="color9 f14" style={{margin:0}}><span>买家配货： </span><span>{el.NUM}</span></p>
                                    <p className="color9 f14" style={{margin:0}}><span>可配库存： </span><span>{el.WAREHOUSE_STOCK}</span></p>
                                </div>
                            </div>
                        )
                    })
                }
                <div className="plAll border_bottom">
                    <div className='f12'>
                        <span className='color9'>商品总价</span>
                        <div className="fr color9">
                            <span>￥</span>
                            <span>{parseFloat(orderFormdDetails.amount/100).toFixed(2)}</span>
                        </div>
                    </div>
                    {/*<div className='f12'>
                        <span className='color9'>运费(快递)</span>
                        <div className="fr color9">
                            <span>￥</span>
                            <span>{orderFormdDetails.postage}</span>
                        </div>
                    </div>*/}
                    <div className='f12'>
                        <span className='color9'>订单总价</span>
                        <div className="fr color9">
                            <span>￥</span>
                            <span>{parseFloat(orderFormdDetails.amount/100).toFixed(2)}</span>
                        </div>
                    </div>
                </div>
                <SplitLine />
                {/*
                 orderFormdDetails.status == 2?
                 <Link to="/orders/applicationForAfterSales" query={{orderNo:orderFormdDetails.order_no}}>
                 <div className="font14 plAll" style={{textAlign:'right'}}>
                 <button className="plr border_ra color9 font14 border_all mt45">
                 申请售后</button>
                 </div>
                 </Link>
                 :null
                 */}
                <div className="plAll border_bottom font14">
                    <div>
                        <span className="color6">实付款</span>
                        <div className="fr color_yellow">
                            <span>￥</span>
                            <span>{parseFloat(orderFormdDetails.amount/100).toFixed(2)}</span>
                        </div>
                    </div>
                </div>
                <SplitLine />
                {
                    this.state.status!=''?
                        <div className="plAll border_bottom font14">
                            <div>
                                <span className="color6">状态</span>
                                <div className="fr color_yellow">
                                    <span>{this.state.status}</span>
                                </div>
                            </div>
                        </div>
                        :
                        <div>
                            <div className="plAll border_bottom f12 color9">
                                <div>
                                    <span>订单编号</span>
                                    <div className="fr">
                                        <span>{this.props.location.query.orderId}</span>
                                    </div>
                                </div>
                                <div>
                                    <span>创建时间</span>
                                    <div className="fr">
                                        <span>{orderFormdDetails.create_time}</span>
                                    </div>
                                </div>
                                {/*<div>
                                    <span>付款时间</span>
                                    <div className="fr">
                                        <span>{orderFormdDetails.pay_time?orderFormdDetails.pay_time:'未付款'}</span>
                                    </div>
                                </div>*/

                                   /* this.props.location.query.indexStatus != 0?
                                        <div>
                                            <span>发货时间</span>
                                            <div className="fr">
                                                <span>{orderFormdDetails.delivery_time}</span>
                                            </div>
                                        </div>
                                        :null*/
                                }
                            </div>
                        </div>
                }

                {
                    this.props.location.query.indexStatus == 0?

                        <div className="fr font14 plAll">
                            {/*  <Link to="/goPicking" query={{orderNo:orderNo,type:1}}>*/}
                                <button
                                    className="tr ml colorfff changeBorder bgk_lv2 border_4 pa_all di f14"
                                    style={{padding:'3px 10px'}}
                                    onClick = {()=>this.check(orderNo)}
                                >发货</button>
                            {/*</Link>*/}
                        </div>
                        :null
                }


            </div>
        );
    }
}
